import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Welcome from '../components/Welcome.vue'
import Bloglist from '../components/bloglist.vue'
import User from '../components/User.vue'
import Addblog from '../components/Addblog.vue'
import Comment from '../components/Comment.vue'
import Link from '../components/other/Link.vue'
import Tag from '../components/other/Tag.vue'
import Category from '../components/other/Category.vue'
import Main from '../components/home/Main.vue'
Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    { path: '/', redirect: '/main' },
    { path: '/login', component: Login },
    {
      path: '/home',
      component: Home,
      redirect: '/welcome',
      children: [
        { path: '/welcome', component: Welcome },
        { path: '/bloglist', component: Bloglist },
        { path: '/user', component: User },
        { path: '/addblog', component: Addblog },
        { path: '/comment', component: Comment },
        { path: '/category', component: Category },
        { path: '/tag', component: Tag },
        { path: '/link', component: Link }
      ]
    },
    { path: '/main', component: Main }
  ]
})
router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    next()
    const tokenStr = window.sessionStorage.getItem('token')
    if (!tokenStr) return next('/login')
    next()
  } else if (to.path === '/main') {
    next()
  }
})
export default router
